package unitTest;
import java.io.IOException;

import utils.ByteArrUtils;
import utils.ByteTree;
/**
 * unit-test class for ByteTree objects
 * @author yuvalshu
 *
 */
public class ByteTreeTester {

	/**
	 * @param args
	 * @throws IOException 
	 */
	
	public static void main(String[] args) throws IOException {
		// checking internal functionality of byte tree class
//		checkByteTreeFunctions();
		
		// testing ByteTree constructor and getters
		ByteTree bt = null;
		bt = testArrayByteTree();
		ByteTreeTesting(bt);
		bt = testStringByteTree();
		ByteTreeTesting(bt);
		testStringByteTree();	
		testByteTreeCreation();
	}

	public static ByteTree testStringByteTree() throws IOException{
		// string taken from example at page 30 in implementation
		String str = "0000000002010000001c766572696669636174756d2e61726974686d2e4d6f645047726f757000000000040100000041014354c848a190b7b5fbddcd07bed36e59af5a50cc5966b202bba0959ccc42061a2f468f87fa436451bd48d5cb333c0bb0aca763193e70c725495455a99939276f010000004100a1aa642450c85bdafdeee683df69b72cd7ad28662cb359015dd04ace6621030d17a347c3fd21b228dea46ae5999e05d85653b18c9f386392a4aa2ad4cc9c93b701000000410132027413c1464af9b3ebe05f40059902857843365887f3e084e973dfd3da198697724ac422dfce4728c2baa07760b5eae2d709bd7ff4f79d4e71fc9c2d37e267010000000400000001";
		ByteTree bt = new ByteTree(str);
		return bt;
	}
	
	public static ByteTree testArrayByteTree() throws IOException{
		byte[] byteArr = {(byte) 0, (byte)0, (byte)0, (byte)0,(byte)2,
				(byte)0,(byte)0,(byte)0,(byte)0,(byte)2,
					(byte)1,(byte)0,(byte)0,(byte)0,(byte)4,(byte)65,(byte)66,(byte)67,(byte)68,
					(byte)1,(byte)0,(byte)0,(byte)0,(byte)2,(byte)3,(byte)225,
				(byte)1,(byte)0,(byte)0,(byte)0,(byte)2,(byte)1,(byte)1};
		ByteTree bt = new ByteTree(byteArr);
		return bt;
	}
	
	public static void ByteTreeTesting(ByteTree bt){
		System.out.println("printing parameters of bt");
		System.out.println("is bt node? " + bt.isNode());
		System.out.println("how many children? " + bt.getNumOfChildren());
		int i = bt.getNumOfChildren();
		ByteTree btArray[] = new ByteTree[i];
		for (int j=0;j<i;j++){
			btArray[j] = bt.getChild(j);
			System.out.println("is child " + j + " a node? "  + btArray[j].isNode());
			System.out.println("how many children for child " +j+ "?" + btArray[j].getNumOfChildren());
			System.out.println("print string of child " + j + " " + btArray[j].getString());
			System.out.println("print number of child "  + j + " " + btArray[j].getNumber());
		}
	}
	
//	public static void checkByteTreeFunctions() throws IOException{
//		//create an array for Example 1 as shown in paper (page #5).
//		byte[] byteArr = {(byte) 0, (byte)0, (byte)0, (byte)0,(byte)2,
//							(byte)0,(byte)0,(byte)0,(byte)0,(byte)2,
//								(byte)1,(byte)0,(byte)0,(byte)0,(byte)4,(byte)65,(byte)66,(byte)67,(byte)68,
//								(byte)1,(byte)0,(byte)0,(byte)0,(byte)2,(byte)3,(byte)225,
//							(byte)1,(byte)0,(byte)0,(byte)0,(byte)2,(byte)1,(byte)1};
//		// testing internal ByteTree class utility functions
//		int numOfChildren = ByteTree.calcSize(byteArr, 1, 4);
//		int endIndex = ByteTree.getEndObjectIndex(byteArr, 0);
//		int endIndexNodeChild = ByteTree.getEndObjectIndex(byteArr, 5);
//		int endIndexLastLeaf = ByteTree.getEndObjectIndex(byteArr, endIndexNodeChild);
//		System.out.println("the number of children is " + numOfChildren);
//		System.out.println("the endIndex is " + endIndex);
//		System.out.println("the endIndexNodeChild is " + endIndexNodeChild);
//		System.out.println("the endIndexLastLeaf is " + endIndexLastLeaf);
//		
//		byte[] tempArr = ByteTree.trimArray(byteArr,5,endIndexNodeChild);
//		System.out.println("the trimmed firse node itself is");
//		for (int i=0;i<tempArr.length;i++){
//			System.out.print(" " +tempArr[i]);
//		}
//		System.out.println();
//		
//		tempArr = ByteTree.trimArray(tempArr, 10, 14);
//		for (int i=0;i<tempArr.length;i++){
//			System.out.print(" " +tempArr[i]);
//		}
//		System.out.println();
//		
//		String str = new String(tempArr, "ASCII");
//		System.out.println("the trimmed array is " + str);
//	}
	
	public static void testByteTreeCreation() throws IOException{
		byte[] data = {(byte)65,(byte)66,(byte)67,(byte)68};
		ByteTree node = ByteTree.CreateNode();
		ByteTree child = ByteTree.CreateLeaf(data);
		ByteTree newNode = node.AddChildToNode(child);
		newNode = newNode.AddChildToNode(child);
		System.out.println(newNode.isNode());
		System.out.println(newNode.getNumOfChildren());
		System.out.println(newNode.getSize());
		System.out.println(newNode.getChild(0).isNode());
		System.out.println(newNode.getChild(0).getSize());
		System.out.println(newNode.getChild(0).getString());
		System.out.println(newNode.getChild(1).isNode());
		System.out.println(newNode.getChild(1).getSize());
		System.out.println(newNode.getChild(1).getString());
		ByteTree child2 = ByteTree.CreateLeaf(ByteArrUtils.AsciiStringToByteArray("YOFI"));
		ByteTree newTree = ByteTree.CreateNode();
		ByteTree multiChilds = newTree.AddMultiChildsToNode(child2,child);
		System.out.println(multiChilds.isNode());
		System.out.println(multiChilds.getNumOfChildren());
		System.out.println(multiChilds.getSize());
		System.out.println(multiChilds.getChild(0).isNode());
		System.out.println(multiChilds.getChild(0).getSize());
		System.out.println(multiChilds.getChild(0).getString());
		System.out.println(multiChilds.getChild(1).isNode());
		System.out.println(multiChilds.getChild(1).getSize());
		System.out.println(multiChilds.getChild(1).getString());
	}
}
